/**
 * ScanningSelectorThread.java created by zhangzhidong 
 * at 下午10:51:43 2015年9月8日
 */
package fileserver;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Queue;

/**
 * @author Alex
 *
 */
public class ScanningSelectorThread implements Runnable{
	private	boolean isAccepting = false;
	private Selector  selector;
	private Queue<ServerSocketChannel> queue;
	public ScanningSelectorThread(Selector selector,Queue queue){
		this.selector = selector;
		this.queue = queue;
	}
	/**
	 * @Param
	 * @Author Alex
	 */
	
	
	public void run() {
		// TODO Auto-generated method stub
	
			while(true){
				try {
					int n =	selector.select();
					if(n>0){
						Iterator<SelectionKey> iter = 	selector.selectedKeys().iterator();
						if(iter.hasNext()){
							SelectionKey key = iter.next();
							iter.remove();
							if(key.isAcceptable()){
								ServerSocketChannel  sc =(ServerSocketChannel) key.channel();					
										queue.add(sc);					
								System.out.println("add a channel");
							}
						}
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
				
			}
		
		
		
	}
	/**
	 * @return the isAccepting
	 */
	public boolean isAccepting() {
		return isAccepting;
	}
	/**
	 * @param isAccepting the isAccepting to set
	 */
	public void setAccepting(boolean isAccepting) {
		this.isAccepting = isAccepting;
	}

}
